/**
 * Chesney's Fireplace System Includes New Supplier, New Fireplace Item, Supplier and Fireplace Update, 
 * Supplier and Fireplace Reports, Login, main menu and Tutorial.
 * 
 * It also Includes Database connection, Database Extension and the two classes which are MySupplier and My Fireplace.
 *  
 * Copyright (C) Bournemouth University 
 * 
 * @author Ruth Shepperd, Ari Lopez
 * 
 * @version 1.0
 */
package GUI;

import javax.swing.*;
import javax.swing.table.TableColumn;

import Database.Database;


import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.print.PrinterException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.Statement;
import java.util.Vector;
import javax.swing.GroupLayout.Alignment;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.border.BevelBorder;
/**
* Class Report.
* This Class will be used to create a table that will contain the data from 
* the main two tables from the database fireplace and supplier. The report
* generated can be printed by the user.
* 
* @author Ari Lopez and Ruth Shepperd
* 
* @version 1.0
* 
*/
public class Report extends JFrame implements ActionListener {
	//Attributes
	 Vector columnNames = new Vector();
     Vector data = new Vector();
     JPanel panel = new JPanel();   
     JPanel southPanel = new JPanel();
     JScrollPane scrollPane;
     JFrame frame; 
     private JLabel lblFireplaceReport;
     private JButton printButton;
   
     /**
      * Constructor Method to set the query and the title of the report
      * 
      * @param query	Query of the report (String)
      * @param title	Title of the report (String)
      */
     public Report(String query, String title){
		
		southPanel.setSize(800, 100);
		BorderLayout myLayout = new BorderLayout();
		
	        try {
	        	//get the java sql driver
	            Class.forName("com.mysql.jdbc.Driver").newInstance();
	            //connect with the database
	            Connection con = (Connection) DriverManager.getConnection("jdbc:mysql://localhost:3306/programming2", "root", "");
	            //set up variable that will contain sql query
	            String sql = query;
	            Statement statement = (Statement) con.createStatement();
	            //retrieve the data from the database
	            ResultSet resultSet = statement.executeQuery(sql);
	            ResultSetMetaData metaData = resultSet.getMetaData();
	            //get the column names from the database
	            int columns = metaData.getColumnCount();
	            for (int i = 1; i <= columns; i++) {
	                columnNames.addElement(metaData.getColumnName(i));
	            }
	            //populate the table with the data resulted from the query
	            while (resultSet.next()) {
	                Vector row = new Vector(columns);
	                for (int i = 1; i <= columns; i++) {
	                    row.addElement(resultSet.getObject(i));
	                }
	                data.addElement(row);
	            }
	            //close connections
	            resultSet.close();
	            statement.close();
	            con.close();
	        } catch (Exception e) {
	            System.out.println(e);
	        }
	        //set up the table and pass the data and the column names
	        final JTable table = new JTable(data, columnNames);
	        TableColumn column;
	        for (int i = 0; i < table.getColumnCount(); i++) {
	            column = table.getColumnModel().getColumn(i);
	            column.setMaxWidth(800);
	            column.setMinWidth(50);
	        }
	        scrollPane = new JScrollPane(table);
	        frame = new JFrame(title);
	        frame.setDefaultCloseOperation(frame.DISPOSE_ON_CLOSE);
	       	        
	        JPanel panel_1 = new JPanel();
	        panel_1.setBorder(new BevelBorder(BevelBorder.RAISED, null, null, null, null));
	        GroupLayout gl_panel = new GroupLayout(panel);
	        gl_panel.setHorizontalGroup(
	        	gl_panel.createParallelGroup(Alignment.LEADING)
	        		.addGroup(gl_panel.createSequentialGroup()
	        			.addGroup(gl_panel.createParallelGroup(Alignment.LEADING)
	        				.addGroup(gl_panel.createSequentialGroup()
	        					.addContainerGap()
	        					.addComponent(scrollPane, GroupLayout.DEFAULT_SIZE, 764, Short.MAX_VALUE))
	        				.addGroup(gl_panel.createSequentialGroup()
	        					.addGap(165)
	        					.addComponent(panel_1, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)))
	        			.addContainerGap())
	        );
	        gl_panel.setVerticalGroup(
	        	gl_panel.createParallelGroup(Alignment.TRAILING)
	        		.addGroup(gl_panel.createSequentialGroup()
	        			.addContainerGap()
	        			.addComponent(panel_1, GroupLayout.DEFAULT_SIZE, 48, Short.MAX_VALUE)
	        			.addPreferredGap(ComponentPlacement.RELATED)
	        			.addComponent(scrollPane, GroupLayout.PREFERRED_SIZE, 402, GroupLayout.PREFERRED_SIZE)
	        			.addContainerGap())
	        );
	        
	        lblFireplaceReport = new JLabel(title);
	        lblFireplaceReport.setFont(new Font("Tahoma", Font.BOLD, 18));
	        GroupLayout gl_panel_1 = new GroupLayout(panel_1);
	        gl_panel_1.setHorizontalGroup(
	        	gl_panel_1.createParallelGroup(Alignment.LEADING)
	        		.addGroup(Alignment.TRAILING, gl_panel_1.createSequentialGroup()
	        			.addContainerGap(149, Short.MAX_VALUE)
	        			.addComponent(lblFireplaceReport, GroupLayout.PREFERRED_SIZE, 267, GroupLayout.PREFERRED_SIZE)
	        			.addGap(65))
	        );
	        gl_panel_1.setVerticalGroup(
	        	gl_panel_1.createParallelGroup(Alignment.LEADING)
	        		.addGroup(Alignment.TRAILING, gl_panel_1.createSequentialGroup()
	        			.addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
	        			.addComponent(lblFireplaceReport)
	        			.addContainerGap())
	        );
	        panel_1.setLayout(gl_panel_1);
	        panel.setLayout(gl_panel);
	        GroupLayout groupLayout = new GroupLayout(frame.getContentPane());
	        groupLayout.setHorizontalGroup(
	        	groupLayout.createParallelGroup(Alignment.LEADING)
	        		.addGroup(groupLayout.createSequentialGroup()
	        			.addComponent(panel, GroupLayout.PREFERRED_SIZE, 784, GroupLayout.PREFERRED_SIZE)
	        			.addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
	        		.addGroup(Alignment.TRAILING, groupLayout.createSequentialGroup()
	        			.addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
	        			.addComponent(southPanel, GroupLayout.PREFERRED_SIZE, 784, GroupLayout.PREFERRED_SIZE)
	        			.addContainerGap())
	        );
	        groupLayout.setVerticalGroup(
	        	groupLayout.createParallelGroup(Alignment.LEADING)
	        		.addGroup(groupLayout.createSequentialGroup()
	        			.addComponent(panel, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
	        			.addPreferredGap(ComponentPlacement.RELATED, 21, Short.MAX_VALUE)
	        			.addComponent(southPanel, GroupLayout.PREFERRED_SIZE, 94, GroupLayout.PREFERRED_SIZE))
	        );
	        
	        //Print Button
	        printButton = new JButton("Print");
	        printButton.addActionListener(new ActionListener() {
	        	/**
	        	 * Print the report
	        	 */
	        	public void actionPerformed(ActionEvent e) {
	        
					try {
						table.print();
					} catch (PrinterException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}
	        	}
	        });
	        
	        GroupLayout gl_southPanel = new GroupLayout(southPanel);
	        gl_southPanel.setHorizontalGroup(
	        	gl_southPanel.createParallelGroup(Alignment.TRAILING)
	        		.addGroup(gl_southPanel.createSequentialGroup()
	        			.addContainerGap(667, Short.MAX_VALUE)
	        			.addComponent(printButton, GroupLayout.PREFERRED_SIZE, 98, GroupLayout.PREFERRED_SIZE)
	        			.addGap(19))
	        );
	        gl_southPanel.setVerticalGroup(
	        	gl_southPanel.createParallelGroup(Alignment.LEADING)
	        		.addGroup(Alignment.TRAILING, gl_southPanel.createSequentialGroup()
	        			.addContainerGap(38, Short.MAX_VALUE)
	        			.addComponent(printButton)
	        			.addGap(33))
	        );
	        southPanel.setLayout(gl_southPanel);
	        frame.getContentPane().setLayout(groupLayout);
	        frame.setSize(800, 631); //setting frame size
	        frame.setVisible(true);  //setting visibility true
	        frame.setResizable(false);	        
	        
	}
	@Override
	public void actionPerformed(ActionEvent arg0) {
		// TODO Auto-generated method stub
		
	}
}